package com.hkyl.hospital.service.impl;

import com.hkyl.hospital.entity.CaseHistory;
import com.hkyl.hospital.entity.CaseHistoryExample;
import com.hkyl.hospital.entity.Register;
import com.hkyl.hospital.mapper.CaseHistoryMapper;
import com.hkyl.hospital.mapper.RegisterMapper;
import com.hkyl.hospital.service.CaseHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author hyh
 * @date 2023/07/14 15:13:27
 */
@Service
@Transactional
public class CaseHistoryServiceImpl implements CaseHistoryService {

    @Autowired
    private CaseHistoryMapper caseHistoryMapper;
    @Autowired
    private RegisterMapper registerMapper;

    @Override
    public Integer save(CaseHistory caseHistory) {
        caseHistory.setCreateDate(new Date());
        int rows = caseHistoryMapper.insertSelective(caseHistory);
        Register register = new Register();
        register.setStatus(1);
        register.setRegisterNo(caseHistory.getRegisterNo());
        rows += registerMapper.updateByPrimaryKeySelective(register);
        return rows;
    }

    @Override
    public CaseHistory getOne(String registerNo) {
        CaseHistoryExample caseHistoryExample = new CaseHistoryExample();
        caseHistoryExample.or().andRegisterNoEqualTo(registerNo);
        return caseHistoryMapper.selectByExample(caseHistoryExample).get(0);
    }
}
